<?php

// Prevent individual file access
if(!defined('DIR_BASE')) exit;

/**
 * Defines which task has to be executed
 */
class Controller {
	
	/**
	* Execute the task which is given in the param.
	* @param String $task	The task that must be executed.
	*/
	public function execute($task){
		switch($task){
			// login
			case 'login':{
				$usermodel = new UserModel;
				if(!$username = Request::getVar('username')) break;
				if(!$password = Request::getVar('password')) $password = '';
				else $password = md5($password);
				
				$user = $usermodel->login($username, $password);
				if(empty($user)) {
					$this->error('Gebruikersnaam en/of wachtwoord onjuist'); break;
				}
				$_SESSION['userid'] = $user['id'];
				$this->display($user);
				break;
			}
			// logout
			case 'logout':{
				if(isset($_SESSION['userid'])){
					unset($_SESSION['userid']);
					$this->display('');
				}else
					$this->error('Je bent al uitgelogd');
				break;
			}
			// check if user is logged in or not
			case 'guest':{
				$usermodel = new UserModel;
				$userid = $_SESSION['userid'];
				$user = $usermodel->get($userid);
				if(empty($user)) {
					$this->error('Niet ingelogd'); break;
				}
				$this->display($user);
				break;
			}
			// get words
			case 'getWords':{
				$definitionModel = new DefinitionModel;
				if(!$userid = Request::getVar('userid')) $userid = null;
				$results = $definitionModel->getWordList($userid);
				$this->display($results);
				break;
			}
			// get definitions of word given by user etc.
			case 'getWordInfo':{
				$definitionModel = new DefinitionModel;
				if(!$word = Request::getVar('word')){ 
					$this->error('Geen woord opgegeven'); break;
				}
				$userDef = $definitionModel->getUserDefinitions($word);
				$publicDefinition = $definitionModel->getPublicDefinition($word);
				$results = array('list'=>$userDef, 'definition'=>$publicDefinition);
				$this->display($results);
				break;
			}
			// save the public definition
			case 'savePublicDefinition':{
				$definitionModel = new DefinitionModel;
				if(!$definition = Request::getVar('definition')){
					$this->error('Geen definitie opgegeven'); break;
				}
				if(!$word = Request::getWord('word')){ 
					$this->error('Geen woord opgegeven'); break;
				}
				$definitionModel->savePublicDefinition($word, $definition);
				break;
			}
			// save user definition
			case 'saveDefinition':{
				$definitionModel = new DefinitionModel;
				if(!$definition = Request::getVar('definition')){
					$this->error('Geen definitie opgegeven'); break;
				}
				if(!$word = Request::getWord('word')){ 
					$this->error('Geen woord opgegeven'); break;
				}
				$definitionModel->saveUserDefinition($word, $definition);
				break;
			}
			// get list of drawings
			case 'getDrawings':{
				$drawingModel = new DrawingModel;
				if(!$word = Request::getVar('word')) $word = null;
				$imageList = $drawingModel->getImageList($word);
				$this->display($imageList);
				break;
			}
			// save drawing
			case 'saveDrawing':{
				$drawingModel = new DrawingModel;
				$web = false;
				if(!$word = Request::getVar('word')){ 
					$this->error('Geen woord opgegeven om op te zoeken'); break;
				}
				$id = $drawingModel->add($word, false, '');
				$drawingModel->saveImage($id);
				break;
			}
			// delete drawing
			case 'deleteDrawing':{
				$drawingModel = new DrawingModel;
				if(!$imgid = Request::getVar('id')){ 
					$this->error('Geen id opgegeven'); break;
				}
				$drawingModel->delete($imgid);
				break;
			}
			// increase score
			case 'raiseScore':{
				$drawingModel = new DrawingModel;
				if(!$imgid = Request::getVar('imgid')){ 
					$this->error('Er is geen tekening geselecteerd'); break;
				}
				$drawingModel->raiseScore($imgid);
				break;
			}
			// get list of users/pupils
			case 'getPupils':{
				$usermodel = new UserModel;
				if(!$userid = Request::getVar('userid')){
					if(isset($_SESSION['userid'])) 
						$userid = $_SESSION['userid'];
					else
						$userid = 0;
				}
				$results = $usermodel->getList($userid);
				$this->display($results);
				break;
			}
			// unknown
			default:{
				$this->error('Onbekende taak: '.$task);
			}
			// default display
			$this->display('');
		}
	}

	/**
	* Display the results
	* @param Object $results	display the results
	*/	
	public function display($results){
		$dp = View::getInstance();
		$dp->display($results);
	}
	
	/**
	* Display a error
	* @param String $error	Display a error
	*/
	public function error($results){
		$dp = View::getInstance();
		$dp->display($results, false);
	}
}

?>